package com.zhongpengcheng.blog.event.handler;

import com.zhongpengcheng.adapter.RedisAdapter;
import com.zhongpengcheng.blog.event.TimedCacheDataEvent;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

/**
 * 默认事件处理器
 * @author ZhongPengCheng
 * @version 1.0
 * @since 2021-12-04 16:08:00
 */
@Slf4j
@Component
public class RedisEventHandler {

    @Autowired
    private RedisAdapter redisAdapter;

    /**
     * 时效性缓存新增事件处理
     * @param event 时效性缓存事件对象
     */
    @Async
    @EventListener
    public void onTimedCacheDataEvent(TimedCacheDataEvent event) {
        try {
            redisAdapter.setWithExpire(event.getKey(), event.getValue(), event.getTimeout(), event.getUnit());
        } catch (Exception e) {
            log.error("时效性缓存事件处理失败, event={}, error={}", event, e);
        }
    }

}
